package com.zs.toolbox.web2j.system.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.toolbox.web2j.common.exception.WtExceptions;
import com.zs.toolbox.web2j.common.request.WtPageBodyModel;
import com.zs.toolbox.web2j.common.request.WtPageQuerySearchModel;
import com.zs.toolbox.web2j.system.core.entity.WtPostDetailEntity;
import com.zs.toolbox.web2j.system.core.mapper.other.WtPostDetailMapper;
import com.zs.toolbox.web2j.system.core.response.WtPostDetailWithUserInfoEntity;
import com.zs.toolbox.web2j.system.core.service.WtPostDetailService;
import com.zs.toolbox.web2j.system.core.service.WtUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author eric.zhang
 * @since 2018-10-11
 */
@Service
public class WtPostDetailServiceImpl extends ServiceImpl<WtPostDetailMapper, WtPostDetailEntity> implements WtPostDetailService {

    @Autowired
    WtUserService wtUserService;

    @Override
    public void savePost(WtPostDetailEntity entity) {
        //判断父结点是否存在
        if (entity.getPostParent() > 0 && baseMapper.selectCount(new LambdaQueryWrapper<WtPostDetailEntity>()
                .eq(WtPostDetailEntity::getPostParent, entity.getPostParent())) <= 0) {
            throw WtExceptions.E_POST_NOEXIST;
        }

        if (null != entity.getPostId()) {
            WtPostDetailEntity wtPostDetailEntity = baseMapper.selectById(entity.getPostId());
            if (!wtPostDetailEntity.getUserId().equals(entity.getUserId())) {
                throw WtExceptions.E_USER_NO_PERMISSION;
            }
        }

        this.saveOrUpdate(entity);
    }

    @Override
    @Transactional
    public WtPostDetailWithUserInfoEntity fetchPostDetailByIdAndRead(Long postId) {
        WtPostDetailEntity wtPostDetailEntity = this.getById(postId);

        if (wtPostDetailEntity == null) {
            throw WtExceptions.E_POST_NOEXIST;
        }

        WtPostDetailWithUserInfoEntity wtPostDetailWithUserInfoEntity = createAndSetWtPostDetailWithUserInfoEntity(wtPostDetailEntity);

        updateAddReadNumById(postId);

        return wtPostDetailWithUserInfoEntity;
    }



    @Override
    public void updateAddReadNumById(Long postId) {
        WtPostDetailEntity wtPostDetailEntity = new WtPostDetailEntity().setPostId(postId);
        wtPostDetailEntity.setReadCount(wtPostDetailEntity.getReadCount() + 1);
        this.updateById(wtPostDetailEntity);
    }

    @Override
    public WtPageBodyModel<WtPostDetailWithUserInfoEntity> pagePost(WtPageQuerySearchModel wtPageQueryModel) {
        /*查询要返回的文章*/
        QueryWrapper<WtPostDetailEntity> condition = new QueryWrapper<WtPostDetailEntity>();

        if (StringUtils.isNotBlank(wtPageQueryModel.getSearchKey())
                && StringUtils.isNotBlank(wtPageQueryModel.getSearchValue())) {
            condition.like(wtPageQueryModel.getSearchKey(), wtPageQueryModel.getSearchValue());
        }

        IPage<WtPostDetailEntity> page = page(new Page<WtPostDetailEntity>(wtPageQueryModel.getPage(), wtPageQueryModel.getRows()), condition);

        List<WtPostDetailWithUserInfoEntity> postDetailWithUserInfoEntities = page.getRecords().stream()
                .map(record -> createAndSetWtPostDetailWithUserInfoEntity(record))
                .collect(Collectors.toList());

        return new WtPageBodyModel(page.getTotal(), page.getCurrent(), postDetailWithUserInfoEntities);
    }

    private WtPostDetailWithUserInfoEntity createAndSetWtPostDetailWithUserInfoEntity(WtPostDetailEntity wtPostDetailEntity) {
        WtPostDetailWithUserInfoEntity wtPostDetailWithUserInfoEntity = new WtPostDetailWithUserInfoEntity();
        BeanUtils.copyProperties(wtPostDetailEntity, wtPostDetailWithUserInfoEntity);

        if (wtPostDetailWithUserInfoEntity.getUserId() > 0) {
            wtPostDetailWithUserInfoEntity
                    .setAuthorName(wtUserService.getById(wtPostDetailEntity.getUserId()).getNickname());
        } else {
            wtPostDetailWithUserInfoEntity.setAuthorName("系统");
        }
        return wtPostDetailWithUserInfoEntity;
    }

}
